home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
102_01
/
bugs.c
< prev
next >
Wrap
Text File
|
1984-02-10
|
8KB
|
350 lines
/*
"Bugs"
written by Steve Ward for the H19/H89 display terminal
BD says..."This one is WIERD!!!"
Usage:
A>bugs [-[w][c]] [N] <cr>
where N sets number of bugs (up to NBUGS); c starts them
out at the center, and w puts up a wall around the screen
that bugs can't go through (except to come IN...so use -c
also if you want an integral wall)
*/
#define NBUGS 25
#define BOT 0
#define LEFT 0
#define RADIUS2 21
#define CONTROL_C 3
#define putchar(c) bios(4,c)
int Top, /* Pagesize-1 */
Right; /* Linelength-2 */
struct bug {
int X,Y;
int Dir; /* 0-down, 1-left, 2-up, 3-right. */
int State; } bugs[NBUGS];
char Wflg, Cflg;
int CurX, CurY;
int CBugs;
int XMotion[20], YMotion[20];
step(bb)
struct bug *bb;
{ switch ((*bb).State) {
case 0: r(bb,1,0,' ');
r(bb,1,-1,'\\'); (*bb).State++; break;
case 1: r(bb,-1,0,' ');
r(bb,-1,-1,'/');
(*bb).State++; break;
case 2: r(bb,1,1,' ');
r(bb,1,0,'\\');
(*bb).State++; break;
case 3: r(bb,-1,1,' ');
r(bb,-1,0,'/');
(*bb).State++; break;
case 4: r(bb,0,-1,'0');
r(bb,0,0,'O');
r(bb,1,-1,'/');
r(bb,-1,-1,'\\');
r(bb,1,0,'/');
r(bb,-1,0,'\\');
(*bb).State++; break;
case 5: r(bb,1,2,' ');
r(bb,1,1,'\\');
(*bb).State++; break;
case 6: r(bb,-1,2,' ');
r(bb,-1,1,'/');
(*bb).State++; break;
case 7: r(bb,1,1,'/');
r(bb,0,1,' ');
r(bb,-1,1,'\\');
switch (((*bb).Dir) & 03) {
case 0: (*bb).Y--; break;
case 2: (*bb).Y++; break;
case 1: (*bb).X++; break;
case 3: (*bb).X--; break; }
(*bb).State = 0; break;
/* Diagonal movement: */
case 20: r(bb,1,1,' ');
r(bb,1,0,'-');
(*bb).State++; break;
case 21: r(bb,-1,-1,' ');
r(bb,0,-1,'|');
(*bb).State++; break;
case 22: r(bb,0,1,' ');
r(bb,1,1,'/');
(*bb).State++; break;
case 23: r(bb,-1,0,' ');
r(bb,-1,-1,'/');
(*bb).State++; break;
case 24: r(bb,1,-1,'0');
r(bb,0,0,'O');
r(bb,1,1,' ');
r(bb,0,1,'|');
r(bb,-1,-1,' ');
r(bb,-1,0,'-');
r(bb,1,0,'|');
r(bb,0,-1,'-');
(*bb).State++; break;
case 25: r(bb,-1,2,' ');
r(bb,0,2,'/');
(*bb).State++; break;
case 26: r(bb,-2,1,' ');
r(bb,-2,0,'/');
(*bb).State++; break;
case 27: r(bb,-1,1,' ');
r(bb,0,2,' ');
r(bb,-2,0,' ');
r(bb,1,0,'|');
r(bb,0,-1,'-');
switch (((*bb).Dir)& 03) {
case 0: (*bb).X++; (*bb).Y--; break;
case 1: (*bb).X++; (*bb).Y++; break;
case 2: (*bb).X--; (*bb).Y++; break;
case 3: (*bb).X--; (*bb).Y--; break; }
(*bb).State = 20; break;
/* turn from diag to orthogonal (45 deg CCW) */
case 40: r(bb,-1,0,' ');
r(bb,-2,0,'/');
(*bb).State++; break;
case 41: r(bb,-1,0,'O');
r(bb,-1,2,' ');
r(bb,-1,1,'|');
r(bb,-2,0,'\\');
r(bb,-2,1,'\\');
(*bb).State++; break;
case 42: r(bb,1,1,' ');
r(bb,0,1,'\\');
r(bb,-1,1,'\\');
r(bb,-2,0,' ');
r(bb,-2,-1,'/');
r(bb,0,-1,'/');
(*bb).Dir = (((*bb).Dir)+1) & 03;
(*bb).State = 0; break;
/* Turn from ortho to diagonal: */
case 50: r(bb,-1,0,' ');
r(bb,-1,-1,'/');
(*bb).State++; break;
case 51: r(bb,-1,1,' ');
r(bb,-1,0,'/');
(*bb).State++; break;
case 52: r(bb,1,2,' ');
r(bb,0,1,'|');
r(bb,-1,1,'O');
r(bb,1,0,' ');
r(bb,-1,2,' ');
r(bb,0,2,'/');
r(bb,-1,0,' ');
r(bb,-2,0,'/');
r(bb,-2,1,'-');
(*bb).State++; break;
case 53: r(bb,0,2,' ');
r(bb,-1,2,'|');
r(bb,-2,0,' ');
r(bb,-1,0,'-');
(*bb).Dir = (((*bb).Dir) | 04);
(*bb).State = 20; break; }
}
mkbug(bb,xp,yp,direc)
struct bug (*bb);
{ (*bb).X = xp;
(*bb).Y = yp;
(*bb).State = 0;
(*bb).Dir = direc;
if (direc<4) {
r(bb,0,0,'0');
r(bb,0,1,'O');
r(bb,1,0,'/');
r(bb,1,1,'/');
r(bb,1,2,'/');
r(bb,-1,2,'\\');
r(bb,-1,1,'\\');
r(bb,-1,0,'\\'); }
else {
(*bb).State = 20;
r(bb,0,0,'0');
r(bb,1,1,'/');
r(bb,-1,-1,'/');
r(bb,0,1,'|');
r(bb,-1,0,'-');
r(bb,-1,1,'O');
r(bb,-1,2,'|');
r(bb,-2,1,'-'); }
}
r(bb,dx,dy,ch)
struct bug (*bb);
char ch;
{ int tx,ty,direc,xdist,ydist;
char buf[4];
direc = ((*bb).Dir) & 03;
if ((direc == 1) || (direc == 3))
{ switch (ch) {
case '/': ch = '\\'; break;
case '\\': ch = '/'; break;
case '|': ch = '-'; break;
case '-': ch = '|'; break;
default: break; }}
switch (direc) {
case 0: tx = dx+(*bb).X; ty = dy+(*bb).Y; break;
case 2: tx = (*bb).X-dx; ty = (*bb).Y-dy; break;
case 1: tx = (*bb).X-dy; ty = (*bb).Y+dx; break;
case 3: tx = (*bb).X+dy; ty = (*bb).Y-dx; break; }
placech(ch,tx,ty); }
placech(ch,tx,ty)
char ch;
{ int xdist,ydist;
if ((tx<LEFT) || (tx>Right) || (ty<BOT) || (ty>Top)) return;
xdist = CurX-tx; ydist = CurY-ty;
if (xdist<0) xdist = -xdist;
if (ydist<0) ydist = -ydist;
if ((ydist+xdist)>2)
{ putchar('\033');
putchar('Y');
putchar(040 + Top - ty);
putchar(040 + tx);
CurX=tx; CurY=ty; }
while (CurX<tx)
{ putchar('\033'); putchar('C'); CurX++; }
while (tx<CurX)
{ putchar('\033'); putchar('D'); CurX--; }
while (CurY<ty)
{ putchar('\033'); putchar('A'); CurY++; }
while (ty<CurY)
{ putchar('\033'); putchar('B'); CurY--; }
putchar(ch);
CurX++; }
randbug(bb)
struct bug *bb;
{ int x,y,dir;
{ dir = rand()%8;
x = rand()%80;
y = rand()%24;
if (Cflg)
{ x = (Right-LEFT)/2; y = (Top-BOT)/2; }
else switch(dir & 03) {
case 0: y=24+4; break;
case 2: y = -4; break;
case 1: x = -4; break;
case 3: x=80+4; break; }
mkbug(bb,x,y,dir); }}
alive(bb)
struct bug *bb;
{ int px,py;
px = (*bb).X; py = (*bb).Y;
switch(((*bb).Dir) & 03) {
case 0: return(py>=BOT-4);
case 1: return(px<=Right+6);
case 2: return(py<=Top+4);
case 3: return(px>=LEFT-4); }}
turn(bb)
struct bug *bb;
{ switch ((*bb).State) {
case 0: (*bb).State = 50; return;
case 20: (*bb).State = 40; return;
default: return; }}
main(argc,argv)
char **argv;
{ int i,j,xdist,ydist,xmot,ymot;
char *arg;
initw(XMotion, "0,1,0,-1,1,1,-1,-1");
initw(YMotion, "-1,0,1,0,-1,1,1,-1");
CurX = 1000; CurY = 1000;
Wflg = 0; Cflg = 0;
CBugs = 5;
Top = 23; Right = 78;
srand1("Are you ready to be driven buggy? ");
getchar();
for (i=1; i<argc; i++)
{ arg = argv[i];
if (arg[0] == '-')
for (j=1; arg[j]; j++) switch(arg[j]) {
case 'W': Wflg++; break;
case 'C': Cflg++; break;
default: printf("bugs: Illegal option\n",21); exit();}
else CBugs = atoi(arg); }
if (CBugs>NBUGS) CBugs=NBUGS;
if (Wflg)
{ for (i=LEFT; i<Right; i++) placech('-',i,Top);
for (i=Top; i>BOT; i--) placech('|',Right,i);
for (i=LEFT; i<Right; i++) placech('-',i,BOT);
for (i=Top; i>BOT; i--) placech('|',LEFT,i);
}
puts("\33x5"); /* turn off cursor */
for (i=0; i<CBugs; i++)
randbug(&bugs[i]);
while(1)
{
if (bios(2)) if (bios(3) == CONTROL_C)
exit(puts("\33y5\33E"));
i = rand()%CBugs;
if (alive(&bugs[i]))
{ step(&bugs[i]);
j = (bugs[i]).State;
xmot = XMotion[(bugs[i]).Dir];
ymot = YMotion[(bugs[i]).Dir];
if ((j == 0) || (j == 20))
{ if (Wflg) {
xdist = (bugs[i]).X;
ydist = xdist-Right;
if ((xmot>0) && (ydist<0) && (ydist*ydist < RADIUS2))
turn(&bugs[i]);
ydist = xdist-LEFT;
if ((xmot<0)&&(ydist>0) && (ydist*ydist < RADIUS2))
turn(&bugs[i]);
xdist = (bugs[i]).Y;
ydist = xdist-Top;
if ((ymot>0)&&(ydist<0) && (ydist*ydist < RADIUS2))
turn(&bugs[i]);
ydist = xdist-BOT;
if ((ymot<0)&&(ydist>0) && (ydist*ydist < RADIUS2))
turn(&bugs[i]); }}
if ((j == 0) || (j == 20))
{ for (j=0;j<CBugs;j++)
{ if (j==i) continue;
xdist = (bugs[j]).X - (bugs[i]).X;
ydist = (bugs[j]).Y - (bugs[i]).Y;
if ((xdist*xdist+ydist*ydist) < 21)
{ if (((xdist*xmot)<=0) && ((ydist*ymot)<=0))
continue;
turn(&bugs[i]);
break; }}
if (!(rand()%15))
turn(&bugs[i]);}}
else randbug(&bugs[i]); }}